package littlecyberwar.model;

import java.util.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.*;

import littlecyberwar.ModelChangedListener;
import littlecyberwar.ui.*;
import littlecyberwar.util.Util;
import littlecyberwar.tool.*;

/**
 * Title:        LittleCyberWar
 * Description:  A online PBEM or PBC (play be Chat) Miniature utility
 * Copyright:    Copyright (c) 2001
 * Company:      S&S Enterprise
 * @author Milton Soong
 * @version 1.0
 */

public class PlayingBoardModel implements iModelFacade {
  ArrayList allElements;
  ArrayList modelChangedSubscribers;

  public PlayingBoardModel() {
    allElements= new ArrayList();
    modelChangedSubscribers = new ArrayList();
  }

  public void addElement ( Element e ) {
    allElements.add(e);
    Integer layerIndex = (Integer)e.getNewProps(Util._layer);
    if ( layerIndex == null ){
	    boolean ret = LayeredUnitHandler.addElementToDefault(e);
	    if ( ret == false ){
	    	System.out.println("can not find a enabled layer");
	    }
    } else {
    	LayeredUnitHandler.addElementToLayer(e, layerIndex);
    }
  }
  
  // take element off board
  public void removeElement( Element e ){
	allElements.remove(e);
	e.setOnboard(false);
	LayeredUnitHandler.removeElement(e);
	return;
  }

  /**
   * this version is used by Placement, use existing transform
   */
  public void addSetElement( Element e ) {
     allElements.add(e);
     boolean ret = LayeredUnitHandler.addElementToDefault(e);
     if ( ret == false ){
     	System.out.println("can not find a enabled layer");
     }   
  }

  public ArrayList getAllElements() {
    return ( allElements );
  }

  public void modelChanged() {
    fireModelChangedNotification();
  }

  public void modelChanged(Rectangle r) {
    System.out.println(this.toString()+": TBD");
  }

  public void subscribeModelChange(ModelChangedListener listener) {
    modelChangedSubscribers.add(listener);
  }

  private void fireModelChangedNotification() {
    for ( int i=0; i < modelChangedSubscribers.size(); i++ ) {
      ModelChangedListener view =
        (ModelChangedListener)modelChangedSubscribers.get(i);
      view.modelChanged();
    }
  }
}